package com.springboot.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 自定义元数据填充器
 */
@Slf4j
@Component
public class MyMetaObjectHandle implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
         // 新增时自动填充 createtime, updatetime 字段
       metaObject.setValue("createTime", LocalDateTime.now());
       metaObject.setValue("updateTime", LocalDateTime.now());
        Long userId1 = BaseContext.getUserId();
        metaObject.setValue("createUser", userId1);
        metaObject.setValue("updateUser", userId1);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        Long userId = BaseContext.getUserId();
        long id = Thread.currentThread().getId();
        log.info("updateFill线程id：{}", id);
       metaObject.setValue("updateUser", userId);
    }
}
